<template>
  <div class="users-wrapper">
    <!-- 搜索 -->
    <div class="search-wrapper">
      <el-input
        v-model="queryModel.query"
        class="input-with-select"
        placeholder="请输入搜索内容"
      >
        <template #append>
          <el-button
            @click="
              queryModel.pagenum = 1;
              getList();
            "
            ><el-icon><Search /></el-icon
          ></el-button>
        </template>
      </el-input>
    </div>
    <!-- 表格 -->
    <Table :lists="list" :column="col">
      <!-- 是否付款 -->
      <template v-slot:pay="{ row: { pay_status } }">
        <el-tag :type="colors[pay_status]">{{ levels[pay_status] }}</el-tag>
      </template>
      <!-- 按钮 -->
      <template v-slot:action="{ row }">
        <el-button
          size="small"
          type="primary"
          @click="
            model = { ...row };
            dialogFormVisible = true;
          "
          >修改地址</el-button
        >
        <el-button size="small" type="success" @click="getAddressList(row.id)"
          >查看地址</el-button
        >
      </template>
    </Table>

    <!-- 弹出框 -->
    <el-dialog
      v-model="dialogFormVisible"
      title="修改地址"
      :close-on-click-modal="false"
    >
      <el-form
        ref="addref"
        :model="model"
        :rules="rules"
        label-width="120px"
        status-icon
      >
        <el-form-item label="详细地址" prop="username">
          <el-input v-model="model.username" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="getAddOrders">确定</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 分页 -->
    <el-pagination
      @size-change="
        (v) => {
          queryModel.pagesize = v;
          getList();
        }
      "
      @current-change="
        (v) => {
          queryModel.pagenum = v;
          getList();
        }
      "
      :current-page="queryModel.pagenum"
      :page-sizes="[5, 10, 15, 20, 25, 30, 35, 40]"
      :page-size="queryModel.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
      <!-- :page-sizes="[1, 2, 3, 4]"-->
    </el-pagination>

    <!-- 订单地址 -->
    <el-dialog v-model="addressvisible" title="查看订单地址">
      <el-timeline :reverse="reverse">
        <el-timeline-item
          v-for="(activity, index) in address"
          :key="index"
          :timestamp="activity.time"
        >
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import { reactive, toRefs, ref, onMounted, computed } from "vue";
import api from "@/http/order.js";

export default {
  name: "users",
  setup() {
    /**
     * 定义数据
     */
    const state = reactive({
      levels: ["未付款", "已付款"],
      colors: ["danger", "warning"],
      /**
       * 订单数据
       */
      address: [],
      /**
       * 订单地址
       */
      addressvisible: false,
      /**
       * 表单验证规则
       */
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
          {
            min: 2,
            max: 10,
            message: "用户名的长度在2～10个字",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
          {
            min: 6,
            max: 18,
            message: "用户名的长度在6～18个字",
            trigger: "blur",
          },
        ],
        email: [
          {
            required: true,
            message: "请输入邮箱",
            trigger: "blur",
          },
          {
            type: "email",
            message: "请输入合法邮箱",
            trigger: "blur",
          },
        ],
        mobile: [
          {
            required: true,
            message: "请输入手机号",
            trigger: "blur",
          },
          {
            pattern: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/,
            message: "请输入正确手机号",
            trigger: "blur",
          },
        ],
      },
      /**
       * 添加模型
       */
      model: {},
      /**
       * 控制弹出框显示隐藏
       */
      dialogFormVisible: false,
      /**
       * 搜素数据
       */
      queryModel: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      /**
       * 总条数
       */
      total: 0,
      /**
       * 用户模型
       */
      list: [],
      /**
       * 表格配置项
       */
      col: [
        {
          type: "index",
          label: "#",
        },
        {
          prop: "order_number",
          label: "订单编号",
          width: "250",
          align: "center",
        },
        {
          prop: "order_price",
          label: "订单价格",
          align: "center",
          width: "120",
        },
        {
          label: "是否付款",
          slot: "pay",
          align: "center",
          width: "120",
        },
        {
          label: "是否发货",
          prop: "is_send",
          align: "center",
          width: "120",
        },
        {
          label: "下单时间",
          prop: "create_time",
          align: "center",
        },
        {
          label: "操作",
          slot: "action",
          align: "center",
        },
      ],
    });

    /**
     * 地址
     */
    let getAddressList = async (id) => {
      const res = await api.AddressList(id);
      state.addressvisible = true;
      console.log(res);
      if (res.meta.status == 200) {
        state.address = res.data;
      }
    };
    /**
     * 添加用户
     */
    let getAddOrders = async () => {
      const res = await api.UpdataOrders(state.model);

      if (res.meta.status == 200 || res.meta.status == 201) {
        state.dialogFormVisible = false;
        getList();
      }
    };
    /**
     * 获取数据
     */
    let getList = async () => {
      const res = await api.OrdersList(state.queryModel);
      console.log(res, "用户");
      if (res.meta.status == 200) {
        state.list = res.data.goods;
        state.total = res.data.total;
      }
      console.log(state.list, 111);
    };

    onMounted(() => {
      getList();
    });

    return {
      ...toRefs(state),
      getList, //渲染
      getAddOrders, //添加 编辑 订单
      getAddressList, //地址
    };
  },
};
</script>

<style lang="scss" scoped>
* {
  box-sizing: border-box;
}

.users-wrapper {
  height: 100%;
  // 搜索盒子
  .search-wrapper {
    height: 50px;
    display: flex;
    align-items: center;
    .input-with-select {
      width: 400px;
      height: 40px;
      margin-right: 20px;
    }
  }
  // 表格
  .el-table {
    margin-top: 10px;
    margin-bottom: 15px;
  }
}
</style>